//举报方式 我的举报
var api = require('../util/api.js');
Page({
  data: {

  },

  onLoad: function (options) {
    let _this = this;
    let chain = api.chain();
    if (api.isBlank(chain.jbfsId)) {
      _this.setData({
        isEdit: false,
        item: {
          ifAnonymous: chain.ifAnonymous
        }
      });
    } else {
      api.get(`${api.url.jbfs}/${chain.jbfsId}`).then(res => {
        _this.setData({
          item: res.data,
          isEdit: true
        });
      })
    }
  },
  formSubmit: function (e) {
    const _this = this;
    let params = e.detail.value;
    let title = _this.checkParams(params);
    if (!api.isBlank(title)) {
      wx.showToast({
        title: title,
        icon: 'none'
      });
      return false
    }
    if (_this.data.isEdit) {
      api.put(api.url.jbfs, params).then(res => {
        api.back("保存成功", "success");
      })
    } else {
      api.post(api.url.jbfs, params).then(res => {
        api.back("保存成功", "success");
      });
    }
  },
  checkParams(params) {
    if (api.isBlank(params.title)) {
      return '标题未填写';
    }
    if (api.isBlank(params.content)) {
      return '举报内容未填写';
    }
    if (!this.data.item.ifAnonymous && api.isBlank(params.contactWay)) {
      return '联系方式未填写';
    }
  },
  ifAnonymousChange(e){
    let item = this.data.item;
    item.ifAnonymous = e.detail.value;
    this.setData({
      item: item
    });
  }
})